#include "manualcontrol.h"
#include "ui_manualcontrol.h"
#include"maininterface.h"

Q_GLOBAL_STATIC(ManualControl,theInstance)
ManualControl::ManualControl(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ManualControl)
{
    ui->setupUi(this);
    initManualControl();
    //空气湿度
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG1_1,this,&ManualControl::controlIrrigationEquipment);
    connect(timerG1_1, &QTimer::timeout, this, &ManualControl::openIrrigationEquipment);
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG2_1,this,&ManualControl::controlVentilatingEquipmentG2_1);
    connect(timerG2_1, &QTimer::timeout, this, &ManualControl::openVentilatingEquipmentG2_1);
//CO2
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG3,this,&ManualControl::controlCO2InterpolatorEquipment_G3);
    connect(timerG3, &QTimer::timeout, this, &ManualControl::openCO2InterpolatorEquipment_G3);
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG2_2,this,&ManualControl::controlVentilatingEquipmentG2_2);
    connect(timerG2_2, &QTimer::timeout, this, &ManualControl::openVentilatingEquipmentG2_2);

//空气温度
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG6,this,&ManualControl::controlheatingEquipment);
    connect(timerG6, &QTimer::timeout, this, &ManualControl::openHeatingEquipment);
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG7,this,&ManualControl::controlairCoolerEquipment);
    connect(timerG7, &QTimer::timeout, this, &ManualControl::openairCoolerEquipment);

//光照
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG5,this,&ManualControl::controlLightingEquipment_G5);
    connect(timerG5, &QTimer::timeout, this, &ManualControl::openLightingEquipment_G5);
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG4,this,&ManualControl::controlRollUpDoorEquipment_G4);
    connect(timerG4, &QTimer::timeout, this, &ManualControl::openRollUpDoorEquipment_G4);
 //土壤

    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG1_2,this,&ManualControl::controlIrrigationEquipmentG1_2);
    connect(timerG1_2, &QTimer::timeout, this, &ManualControl::openIrrigationEquipmentG1_2);
    connect(MainInterface::instance(),&MainInterface::sendPushbuttonG2_3,this,&ManualControl::controlVentilatingEquipmentG2_3);
    connect(timerG2_3, &QTimer::timeout, this, &ManualControl::openVentilatingEquipmentG2_3);

}

ManualControl::~ManualControl()
{
    delete ui;
}

ManualControl *ManualControl::instance()
{
return theInstance;

}

void ManualControl::initManualControl()
{
 timerG1_1=new QTimer(this);
 timerG1_1->setInterval(2000);

 timerG1_2=new QTimer(this);
 timerG1_2->setInterval(2000);

 timerG2_1=new QTimer(this);
 timerG2_1->setInterval(2000);

 timerG2_2=new QTimer(this);
 timerG2_2->setInterval(2000);

 timerG2_3=new QTimer(this);
 timerG2_3->setInterval(2000);

 timerG3=new QTimer(this);
 timerG3->setInterval(2000);

 timerG4=new QTimer(this);
 timerG4->setInterval(2000);

 timerG5= new QTimer(this);
 timerG5->setInterval(2000); // 设置定时周期为2秒

 timerG6= new QTimer(this);
 timerG6->setInterval(2000); // 设置定时周期为2秒

 timerG7= new QTimer(this);
 timerG7->setInterval(2000); // 设置定时周期为2秒
}

void ManualControl::controlheatingEquipment(bool status)
{


    if(status)
    {
        //开启加热

        timerG6->start();
    }
    else
    {

        //关闭加热
        timerG6->stop();
        emit sendCloseHeatingEquipment();
        qDebug()<<"关闭加热器........";

    }
}

void ManualControl::openHeatingEquipment()
{
    i++;
    qDebug()<<"开始加热器........"<<i<<"s"<<endl;
    emit sendRaiseAirTemperature();
}

void ManualControl::controlairCoolerEquipment(bool status)
{

    if(status)
    {
        //开启降温

        timerG7->start();
    }
    else
    {

        //关闭降温
        timerG7->stop();
        emit sendCloseReduceEquipment();
        qDebug()<<"关闭冷风机........";

    }


}

void ManualControl::openairCoolerEquipment()
{
    i++;
    qDebug()<<"开始加冷风机........"<<i<<"s"<<endl;
    emit sendReduceAirTemperature();
}

void ManualControl::controlIrrigationEquipment(bool status)
{
    if(status)
    {
        //开启打开灌溉设备

        timerG1_1->start();
    }
    else
    {

        //关闭灌溉设备
        timerG1_1->stop();
        emit sendCloseIrrigationEquipment();//关闭灌溉设备
        qDebug()<<"关闭灌溉设备........";

    }
}

void ManualControl::openIrrigationEquipment()
{
    qDebug()<<"打开灌溉设备";
    emit sendOpenIrrigationEquipment();
}

void ManualControl::controlVentilatingEquipmentG2_1(bool status)
{
    if(status)
    {
        //开启通风设备除湿

        timerG2_1->start();
    }
    else
    {

        //关闭通风设备除湿
        timerG2_1->stop();
        emit sendCloseVentilatingEquipmentG2_1();//关闭灌溉设备
        qDebug()<<"关闭通风设备除湿.......";

    }
}

void ManualControl::openVentilatingEquipmentG2_1()
{
 //开启通风设备除湿
     qDebug()<<"开启通风设备除湿";
     emit sendOpenVentilatingEquipmentG2_1();

}

void ManualControl::controlVentilatingEquipmentG2_2(bool status)
{
    if(status)
    {
        //开启降低CO2浓度通风设备

        timerG2_2->start();
    }
    else
    {

        //关闭降低CO2浓度通风设备
        timerG2_2->stop();
        emit sendCloseVentilatingEquipmentG2_2();
        qDebug()<<"关闭降低CO2浓度通风设备.......";

    }
}

void ManualControl::openVentilatingEquipmentG2_2()
{
    //开启通风设备降低CO2浓度
        qDebug()<<"开启降低CO2浓度通风设备";
        emit sendOpenVentilatingEquipmentG2_2();
}

void ManualControl::controlCO2InterpolatorEquipment_G3(bool status)
{
    if(status)
    {
        //开启增加CO2浓度设备

        timerG3->start();
    }
    else
    {

        //关闭增加CO2浓度设备
        timerG3->stop();
        emit sendCloseCO2InterpolatorEquipment_G3();
        qDebug()<<"关闭增加CO2浓度设备.......";

    }
}

void ManualControl::openCO2InterpolatorEquipment_G3()
{
    //开启CO2添加器
        qDebug()<<"开启CO2添加器";
        emit sendOpenCO2InterpolatorEquipment_G3();
}

void ManualControl::controlLightingEquipment_G5(bool status)
{
    if(status)
    {
        //开启补光设备

        timerG5->start();
    }
    else
    {

        //关闭补光设备
        timerG5->stop();
        emit sendCloseLightingEquipment_G5();
        qDebug()<<"关闭补光设备.......";

    }
}

void ManualControl::openLightingEquipment_G5()
{
    //开启补光设备
        qDebug()<<"开启补光灯";
        emit sendOpenLightingEquipment_G5();
}

void ManualControl::controlRollUpDoorEquipment_G4(bool status)
{
    if(status)
    {
        //开启遮光帘

        timerG4->start();
    }
    else
    {

        //关闭遮光帘
        timerG4->stop();
        emit sendCloseRollUpDoorEquipment_G4();
        qDebug()<<"关闭遮光帘.......";

    }
}

void ManualControl::openRollUpDoorEquipment_G4()
{
    //开启遮光帘
        qDebug()<<"开启遮光帘";
        emit sendOpenRollUpDoorEquipment_G4();
}

void ManualControl::controlIrrigationEquipmentG1_2(bool status)
{
    if(status)
    {
        //开启灌溉设备--土壤

        timerG1_2->start();
    }
    else
    {

        //关闭遮光帘
        timerG1_2->stop();
        emit sendCloseIrrigationEquipmentG1_2();
        qDebug()<<"关闭灌溉设备--土壤.......";

    }
}

void ManualControl::openIrrigationEquipmentG1_2()
{
    //开启灌溉设备--土壤
        qDebug()<<"开启灌溉设备--土壤";
        emit sendOpenIrrigationEquipmentG1_2();
}

void ManualControl::controlVentilatingEquipmentG2_3(bool status)
{
    if(status)
    {
        //开启通风设备--土壤

        timerG2_3->start();
    }
    else
    {

        //关闭通风设备--土壤
        timerG2_3->stop();
        emit sendCloseVentilatingEquipmentG2_3();
        qDebug()<<"关闭通风设备--土壤.......";

    }
}

void ManualControl::openVentilatingEquipmentG2_3()
{
     //开启通风设备--土壤
        qDebug()<<"开启通风设备--土壤";
        emit sendOpenVentilatingEquipmentG2_3();
}
